package vip.xiaonuo.supplier.modular.base.categories.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.supplier.modular.base.categories.entity.SupplierCategories;
import vip.xiaonuo.supplier.modular.base.categories.param.SupplierCategoriesAddParam;
import vip.xiaonuo.supplier.modular.base.categories.param.SupplierCategoriesEditParam;
import vip.xiaonuo.supplier.modular.base.categories.param.SupplierCategoriesIdParam;
import vip.xiaonuo.supplier.modular.base.categories.param.SupplierCategoriesPageParam;
import vip.xiaonuo.supplier.modular.base.categories.service.SupplierCategoriesService;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 产品分类控制器
 *
 * @author Mr.wu
 * @date  2025/02/28 14:52
 */
@Tag(name = "产品分类控制器")
@RestController
@Validated
public class SupplierCategoriesController {

    @Resource
    private SupplierCategoriesService supplierCategoriesService;

    /**
     * 获取产品分类分页
     *
     * @author Mr.wu
     * @date  2025/02/28 14:52
     */
    @Operation(summary = "获取产品分类分页")
    @SaCheckPermission("/supplier/base/categories/page")
    @GetMapping("/supplier/base/categories/page")
    public CommonResult<Page<SupplierCategories>> page(SupplierCategoriesPageParam supplierCategoriesPageParam) {
        return CommonResult.data(supplierCategoriesService.page(supplierCategoriesPageParam));
    }

    /**
     * 获取分类树
     *
     * @author Mr.wu
     * @date 2022/10/24 20:00
     */
    @Operation(summary = "获取分类树")
    @SaCheckPermission("/supplier/base/categories/tree")
    @GetMapping("/supplier/base/categories/tree")
    public CommonResult<List<Tree<String>>> tree() {
        return CommonResult.data(supplierCategoriesService.tree());
    }

    /**
     * 添加产品分类
     *
     * @author Mr.wu
     * @date  2025/02/28 14:52
     */
    @Operation(summary = "添加产品分类")
    @CommonLog("添加产品分类")
    @SaCheckPermission("/supplier/base/categories/add")
    @PostMapping("/supplier/base/categories/add")
    public CommonResult<String> add(@RequestBody @Valid SupplierCategoriesAddParam supplierCategoriesAddParam) {
        supplierCategoriesService.add(supplierCategoriesAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑产品分类
     *
     * @author Mr.wu
     * @date  2025/02/28 14:52
     */
    @Operation(summary = "编辑产品分类")
    @CommonLog("编辑产品分类")
    @SaCheckPermission("/supplier/base/categories/edit")
    @PostMapping("/supplier/base/categories/edit")
    public CommonResult<String> edit(@RequestBody @Valid SupplierCategoriesEditParam supplierCategoriesEditParam) {
        supplierCategoriesService.edit(supplierCategoriesEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除产品分类
     *
     * @author Mr.wu
     * @date  2025/02/28 14:52
     */
    @Operation(summary = "删除产品分类")
    @CommonLog("删除产品分类")
    @SaCheckPermission("/supplier/base/categories/delete")
    @PostMapping("/supplier/base/categories/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   List<SupplierCategoriesIdParam> supplierCategoriesIdParamList) {
        supplierCategoriesService.delete(supplierCategoriesIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取产品分类详情
     *
     * @author Mr.wu
     * @date  2025/02/28 14:52
     */
    @Operation(summary = "获取产品分类详情")
    @SaCheckPermission("/supplier/base/categories/detail")
    @GetMapping("/supplier/base/categories/detail")
    public CommonResult<SupplierCategories> detail(@Valid SupplierCategoriesIdParam supplierCategoriesIdParam) {
        return CommonResult.data(supplierCategoriesService.detail(supplierCategoriesIdParam));
    }

    /**
     * 获取分类树选择器
     *
     * @author Mr.wu
     * @date 2022/10/24 20:00
     */
    @Operation(summary = "获取分类树选择器")
    @SaCheckPermission("/supplier/base/categories/categoriesTreeSelector")
    @GetMapping("/supplier/base/categories/categoriesTreeSelector")
    public CommonResult<List<Tree<String>>> categoriesTreeSelector() {
        return CommonResult.data(supplierCategoriesService.categoriesTreeSelector());
    }
}
